package com.ma.gulimall.product.app;

import com.ma.gulimall.product.entity.BrandEntity;
import com.ma.gulimall.product.service.BrandService;
import com.ma.gulimall.utils.PageUtils;
import com.ma.gulimall.utils.R;
import com.ma.gulimall.valid.AddGroup;
import com.ma.gulimall.valid.UpdateGroup;
import com.ma.gulimall.valid.UpdateStatusGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 品牌
 *
 * @author mayongqiang
 * @email 178780494@qq.com
 * @date 2021-06-13 23:17:48
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     * 使用模糊查询  eq id  /   like  name
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){

        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    @GetMapping("/infos")
    public R info(@RequestParam("brandIds") List<Long> brandIds){
        List<BrandEntity> brands = brandService.getBrandsByIds(brandIds);

        return R.ok().put("brands", brands);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:brand:save")
    //Valid 对该api开启校验
    public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand
                  //,BindingResult result
                  ){

        //Map<String, String> map = new HashMap<>();
        //
        //if(result.hasErrors()){
        //
        //    //获取校验错误结果
        //    result.getFieldErrors().forEach((item) ->{
        //        //获取错误提示
        //        String message = item.getDefaultMessage();
        //        //获取到错误的属性的名字
        //        String field = item.getField();
        //        map.put(field,message);
        //
        //    });
        //    return R.error(400,"提交的数据不合法").put("data",map);
        //
        //}else {
        //    brandService.save(brand);
        //    return R.ok();
        //}
            brandService.save(brand);
            return R.ok();

    }

    /**
     * 修改   保证冗余字段得到更新
     * 考虑到冗余表`pms_category_brand_relation`
     * 修改时应同步修改冗余表的内容
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand){
		brandService.updateDetail(brand);

        return R.ok();
    }
    /**
     * 修改状态
     */
    @RequestMapping("/update/status")
    //@RequiresPermissions("product:brand:update")
    public R updateStatus(@Validated({UpdateStatusGroup.class}) @RequestBody BrandEntity brand){
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
